// components/my-component/my-component.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  data: {
    array: ['选项1', '选项2', '选项3'],
    index: 0,
    selectedValue: '',
    showPicker: false,
    showInput: false,
    deleteRoom:false,
    inputValue: "",
    list: [{
        text: 'Item 1'
      },
      {
        text: 'Item 2'
      },
      {
        text: 'Item 3'
      }
    ],
    items: [{
        roomId: 0,
        roomName: '卧室设备',
        totalCount: 0, //商品数量
        productTypesNum: 0, //商品种类数量
        goods: []
      },
      {
        roomId: 1,
        roomName: '书房设备',
        totalCount: 0, //商品数量
        productTypesNum: 0, //商品种类数量
        goods: []
      },
    ],
    roomIdCounter: 2
  },

  methods: {
    bindPickerChange: function (e) {
      const index = e.detail.value;
      const selectedValue = this.data.array[index];
      this.setData({
        index: index,
        selectedValue: selectedValue,
        showPicker: false // 在选择后隐藏下拉框
      });
    },
    showPicker: function () {
      this.setData({
        showPicker: true // 点击时显示下拉框
      });
    },

    //显示输入弹框
    showInputDialog() {
      this.setData({
        showInput: true
      });
    },

    onClose() {
      this.setData({
        showInput: false
      });
    },

    //获取输入框里面的值
    handleInput(e) {
      this.setData({
        inputValue: e.detail.value
      })
    },

    //删除房间
    deleteItem(event) {    
      this.setData({
        deleteRoom: true
      });
    },
    // 确定删除房间
    confirmDelete(event){
      const roomId = event.currentTarget.dataset.roomid;
      console.log("roomId",event.currentTarget.dataset.roomid);
      const newItems = this.data.items.filter(item => item.roomId !== roomId);
          console.log("newItems",newItems);
        this.setData({
          items: newItems,
          deleteRoom: false
        });
        console.log("items",this.data.items);
    },
    //取消删除房间
    cancellation(){
      this.setData({
        deleteRoom: false
      });
    },


    //更新房间数组
    additem() {
      const newItem = {
        roomId: this.data.roomIdCounter++,
        roomName: this.data.inputValue,
        totalCount: 0, // 添加商品数量
        productTypesNum: 0, // 添加商品种类数量
        goods: []
      };
      const newItems = [...this.data.items, newItem];
      if (this.data.inputValue == "") {
        Toast('房间不能为空');
      } else {
        this.setData({
          items: newItems,
          inputValue: '', // 清空输入框
          showInput: false // 关闭弹框
        });
      }
    },

    cancelitem() {
      this.setData({
        showInput: false
      })
    },

    //修改房间名
    changeInput(event) {
      console.log("event", event.currentTarget.dataset.roomid);
      const roomId = event.currentTarget.dataset.roomid
      const newValue = event.detail.value;
      this.setData({
        items: this.data.items.map(item =>
          item.roomId == roomId ? {
            ...item,
            roomName: newValue
          } : item
        )
      });
    },

    //点击确定，关闭弹框
    confirmCloseRoom() {
      const app = getApp(); // 获取小程序实例
      app.globalData.rooms = this.data.items; // 将数据存储到全局变量中
      console.log("item", this.data.items);
      console.log("Stored items to global data:", app.globalData.rooms);
      this.triggerEvent('confirmCloseRoom');
    },

    //点击取消，关闭弹框
    closeRoom() {
      this.triggerEvent('closeRoom');
    }
  },
});